package com.ccjq.jq173.shopping.service.impl;

import com.ccjq.jq173.product.po.Product;
import com.ccjq.jq173.shopping.dao.ShoppingCartDao;
import com.ccjq.jq173.shopping.dao.impl.ShoppingCartDaoImpl;
import com.ccjq.jq173.shopping.po.ShoppingCart;
import com.ccjq.jq173.shopping.service.ShoppingCartService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShoppingCartServiceImpl implements ShoppingCartService {
    private ShoppingCartDao ShoppingCartdao;
    private Product product;
    private ShoppingCart shoppingCart;
    public ShoppingCartServiceImpl()
    {
        ShoppingCartdao=new ShoppingCartDaoImpl();
    }
    @Override
    public List<ShoppingCart> queryShoppings(String userId) {
        List<ShoppingCart> list= ShoppingCartdao.queryByUserId(userId);
        return list;
    }

    @Override
    public double aggregate(List<ShoppingCart> shoppingCarts) {
        product =new Product();
        double zong=0;
        for(Object o:shoppingCarts){
            shoppingCart =(ShoppingCart)o;
            zong+=Double.parseDouble( shoppingCart.getProduct().getPrice())*shoppingCart.getQuantity();
        }
        return zong;
    }

    @Override
    public void deleteShoppingCart(String shoppingCartId, String productId, String userId) {
        ShoppingCartdao.updatShoppingCart(shoppingCartId,productId,userId);
    }

    @Override
    public int updatShoppingCart(int quantity, String productId, String userId) {
        return ShoppingCartdao.saveShoppingCart(quantity,productId,userId);
    }

    @Override
    public int saveShoppingCart(int quantity, String shoppingCarId, String userId, String productId) {

        return ShoppingCartdao.saveShoppingCart( quantity, shoppingCarId,userId,productId);
    }

    @Override
    public ShoppingCart queryByCartId(String shoppingCartId) {
        return ShoppingCartdao.queryByCartId(shoppingCartId);
    }

    @Override
    public int updateStack(String productId, int quantity) {
        return ShoppingCartdao.updateStock( productId, quantity);
    }

    @Override
    public void updateT_ORDER_PRODUCT(String orderId, List<ShoppingCart> list) {
        ShoppingCartdao.updateT_ORDER_PRODUCT( orderId, list);
    }

    @Override
    public int settleAccounts(String orderId, int payment, double amount, String receiver, String province, String city, String county, String address, String zipcode, String telphone, String userId) {
        Map<String,Object> params =new HashMap<String,Object>();
        params.put("orderId",orderId);
        params.put( "payment",payment );
        params.put( "amount",amount );
        params.put( "receiver",receiver );
        params.put( "province",province );
        params.put( "city",city );
        params.put( "county",county );
        params.put( "address",address );
        params.put( "zipcode",zipcode );
        params.put( "telphone",telphone );
        params.put( "userId",userId );
        return ShoppingCartdao.settleAccounts( params );
    }
}
